package com.example.timerecorder;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.HashMap;

public class TagAdapter extends ArrayAdapter {

    private static final String TAG = "TagAdapter";
    private int mSelect;   //选中项
    private String mSelectStr;


    public TagAdapter(Context context, int resource, ArrayList<String> list){
        super(context, resource, list);
    }

    @NonNull
    @Override
    public View getView(int position, @NonNull View convertView, @NonNull ViewGroup parent){
        View itemView = convertView;
        if(itemView == null){
            //利用LayoutInflate把控件所在的布局文件加载到当前类中
            itemView = LayoutInflater.from(getContext()).inflate(R.layout.list_tag, parent, false);
        }

        //单选按钮
        String str = (String) getItem(position);
        TextView radioText = (TextView) itemView.findViewById(R.id.tagRadioText);
        ImageView radioImage = (ImageView) itemView.findViewById(R.id.tagRadioIamge);
        radioText.setText(str);
        radioImage.setColorFilter(Color.LTGRAY);

        //1_不改变输入框点击
        //2_搜索或创建，点击tag，返回保留点击
        if(mSelectStr == null)
            mSelectStr = "";

        if(mSelect == position) {
            Log.i(TAG, "getView-position: 第:   " + position + " 项被点击了");
            Log.i(TAG, "getView-position: 点击项:   " + str);
            mSelectStr = str;
            radioImage.setColorFilter(Color.DKGRAY);
        }
        else if(mSelectStr.equals(str)){
            Log.i(TAG, "getView-selectTag: 第:   " + position + " 项被点击了");
            Log.i(TAG, "getView-selectTag: 点击项:   " + str);
            mSelectStr = str;
            radioImage.setColorFilter(Color.DKGRAY);
        }
        else{
            //未被点击的item
            radioImage.setColorFilter(Color.LTGRAY);
        }

        return itemView;
    }

    //刷新方法
    public void changeSelected(int positon) {
        if (positon != mSelect) {
            mSelect = positon;
            notifyDataSetChanged();
        }
    }

    //设置默认显示颜色
    public void defaultTagColor(){
        mSelect = -1;
    }
    
    //设置之前已点击保存tag
    public void setSelectTag(String str){
        mSelectStr = str;
    }
}
